package gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import javax.media.opengl.GL;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawableFactory;
import javax.media.opengl.GLPbuffer;
import javax.media.opengl.glu.GLU;
import javax.swing.ImageIcon;
import javax.swing.Timer;
import com.sun.opengl.util.Screenshot;
import util.BasicGraphicsJogl;

public class ShapedFrameImage extends ShapedFrame implements ActionListener
{
    private Image image;
    private BufferedImage screenImage;
    private float color;
    public ShapedFrameImage(boolean draggable,String imageName,float color)
    {
        super(draggable);
        this.color = color;
        image = new ImageIcon(imageName).getImage();
        setPreferredSize(new Dimension(500,500));
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Timer timer = new Timer(0,this);
        timer.setRepeats(true);
        timer.setDelay(10);
        timer.start();
        

        GLDrawableFactory factory = GLDrawableFactory.getFactory();
        GLCapabilities glCap = new GLCapabilities();
        glCap.setAlphaBits(8);
        // Without line below, there is an error on Windows.&#10;
        glCap.setDoubleBuffered(false);
        //makes a new buffer&#10;  
        GLPbuffer pBuffer = factory.createGLPbuffer(glCap, null, 500, 500, null);  
        //required for drawing to the buffer&#10;  
        GLContext glContext = pBuffer.createContext(null);
        BasicGraphicsJogl.createInstance(glContext);
        BasicGraphicsJogl basicGraphics = BasicGraphicsJogl.instance();
        
        
        GL gl = basicGraphics.getGL();
        gl.glShadeModel(GL.GL_SMOOTH);
        gl.glClearColor(color, 0.0f, 0.0f, 0.5f);
        gl.glEnable(GL.GL_TEXTURE_2D);
        gl.glEnable (GL.GL_BLEND); 
        gl.glBlendFunc (GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
        gl.glViewport(0, 0, 500, 500);
        gl.glMatrixMode(GL.GL_PROJECTION);
        gl.glLoadIdentity();
        GLU glu = new GLU();
        glu.gluOrtho2D(0, 500, 500, 0);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT);// | GL.GL_DEPTH_BUFFER_BIT);
        screenImage = Screenshot.readToBufferedImage(500,500,true); 

//      // clean the old buffer&#10;     
//      context.destroy();   
//      //context is type GLContext&#10;     
//      buf.destroy();    
//      // buf is type GLPbuffer&#10;  
        setVisible(true);
        
    }
    @Override
    public void paint(Graphics g)
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(screenImage, 0, 0, 500, 500, null);
        
//        g2d.drawImage(image, 0, 0, image.getWidth(null), image.getHeight(null), null);
//        BasicGraphicsJogl basicGraphics = BasicGraphicsJogl.instance();
        
    }
    @Override
    public void actionPerformed(ActionEvent e)
    {

    }
    

}
